﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;

namespace Autenticazione.BusinessObject
{
    public class Atleta
    {
        public string EMail;
        public long Id;
        public string FullName;
        public int Gender;
        public string BirthDayString;
        public int Belt;
        public float Weight;
        public string GrapplingLevel;
        public int MatchFullContact;
        public int MatchLowKick;
        public int MatchK1Rules;

        public Atleta()
        {

        }

        public Atleta(string email, long id, string fullname, int gender, string birthday, int belt, float weight, string grapplinglevel, int matchFullContact, int matchLowKick, int matchK1Rules)
        {
            this.EMail = email;
            this.Id = id;
            this.FullName = fullname.Replace("'", "''");
            this.Gender = gender;
            this.BirthDayString = birthday;
            this.Belt = belt;
            this.Weight = weight;
            this.GrapplingLevel = grapplinglevel;
            this.MatchFullContact = matchFullContact;
            this.MatchLowKick = matchLowKick;
            this.MatchK1Rules = matchK1Rules;
        }

        public DataTable GetTbAtleti(string email)
        {
            string strSQL = string.Format(@"select * from Atleta where Email = '{0}' order by FullName", email);
            DB.DB db = new DB.DB();

            return db.GetTable(strSQL);

        }

        public string Serialize()
        {
            string strSQL = string.Format(@"select * from Atleta order by FullName");
            DB.DB db = new DB.DB();
            DataTable dt = db.GetTable(strSQL);

            dt.TableName = "Atleta";

            System.IO.StringWriter writer = new System.IO.StringWriter();
            
            dt.WriteXml(writer, XmlWriteMode.IgnoreSchema, true);

            return  writer.ToString();
        }

        public List<Atleta> GetAtleti(string email)
        {
            SqlDataReader dr = null;
            List<Atleta> atleti = new List<Atleta>();
            string strSQL = string.Format(@"select * from Atleta where Email = '{0}' order by FullName", email);
            DB.DB db = new DB.DB();

            dr = db.Reader(strSQL);

            if (dr == null)
                return atleti;

            while (dr.Read())
                atleti.Add(new Atleta(dr["Email"].ToString(), long.Parse(dr["Id"].ToString()),
                                dr["FullName"].ToString(),
                                int.Parse(dr["Gender"].ToString()),
                                DateTime.Parse(dr["BirthDay"].ToString()).ToString("dd/MM/yyyy"),
                                int.Parse(dr["Belt"].ToString()),
                                float.Parse(dr["Weight"].ToString()),
                                dr["GrapplingLevel"].ToString(),
                                int.Parse(dr["MatchFullContact"].ToString()),
                                int.Parse(dr["MatchLowKick"].ToString()),
                                int.Parse(dr["MatchK1Rules"].ToString())));

            db.CloseConnection();

            return atleti;
        }

        public bool DeleteAtleta()
        {
            string strSQL = string.Format(@"delete from Atleta where Id = {0}", this.Id.ToString());
            DB.DB db = new DB.DB();
            Fights f = new Fights();
            Boolean deleted = db.ExecuteNonQuery(strSQL);

            if (deleted)
                deleted &= f.DeleteFights(this.EMail, this.Id);
            
            return deleted;
        }

        public bool Salva()
        {
            Boolean saved = false;

            string w = this.Weight.ToString().Replace(",", ".");

            string strSQL = string.Format(@"insert into Atleta (EMail, FullName, Gender, BirthDay, Belt, Weight, GrapplingLevel, MatchFullContact, MatchLowKick, MatchK1Rules) values ('{0}', '{1}', {2}, '{3}', {4}, {5}, '{6}', {7}, {8}, {9})", this.EMail, this.FullName, this.Gender.ToString(), this.BirthDayString, this.Belt.ToString(), w, this.GrapplingLevel, this.MatchFullContact.ToString(), this.MatchLowKick.ToString(), this.MatchK1Rules.ToString());
            DB.DB db = new DB.DB();
            saved = db.ExecuteNonQuery(strSQL);
            
            return saved;
        }

        public bool Update()
        {
            string w = this.Weight.ToString().Replace(",", ".");
            string strSQL = string.Format(@"update Atleta set FullName = '{0}', Gender = {1}, BirthDay = '{2}', Belt = {3}, Weight = {4}, GrapplingLevel = '{5}', MatchFullContact = {6}, MatchLowKick = {7}, MatchK1Rules = {8} where Id = {9}", this.FullName, this.Gender.ToString(), this.BirthDayString, this.Belt.ToString(), w, this.GrapplingLevel, this.MatchFullContact.ToString(), this.MatchLowKick.ToString(), this.MatchK1Rules.ToString(), this.Id.ToString());
            DB.DB db = new DB.DB();
            Boolean saved = db.ExecuteNonQuery(strSQL);

            return saved;
        } 
    }
}